package com.linln.admin.pcs.controller;


import com.linln.admin.system.service.AppService;
import com.linln.common.data.QuerySpec;
import com.linln.common.utils.ValidationUtil;
import com.linln.component.actionLog.annotation.EntityParam;
import com.linln.modules.system.domain.ProjectStage;
import com.linln.modules.system.repository.ProjectStageRepository;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;


/**
 * 项目阶段管理
 *
 */
@Controller
@RequestMapping("/pcs/project/stage")
public class ProjectStageController {

    @Autowired
    private AppService appService;

    @Autowired
    private ProjectStageRepository projectStageRepository;


    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("pcs:projectStage:index")
    public ResponseEntity<?> index(Model model, @EntityParam ProjectStage  projectStage) {

        return appService.layTable(projectStageRepository, before -> {
                    model.addAttribute("projectId", projectStage.getProjectId());
                    // 添加模板变量
                    return model;

                },

                after -> {
                    QuerySpec querySpec = QuerySpec.matching();
                    querySpec.withMatcherValue("projectId", QuerySpec.EQUAL, projectStage.getProjectId().toString());
                    return  QuerySpec.of(querySpec);
                },

                "/pcs/project_stage/index",

                (pageResult) -> {
                    // 在这里处理 pageResult
                    // 例如：修改每一条数据
                }

        );

    }


    /**
     * 跳转到添加页面
     */
    @RequestMapping("/form")
    @RequiresPermissions("pcs:projectStage:add")
    public ResponseEntity<?> form(Model model, @EntityParam ProjectStage projectStage) throws Exception {
        return appService.form(projectStageRepository, "/pcs/project_stage/form",
                (isEdit) -> {
                    model.addAttribute("projectId", projectStage.getProjectId());
                    return model;
                },
                (isEdit, vo) -> {

                    model.addAttribute("vo", vo);
                    // 你可以在这里添加其他逻辑
                    return model;
                },
                (data) -> {
                    // 保存前数据校验
                    List<ValidationUtil.ValidationRule<?>> rules = new ArrayList<>();
                    // 添加邮箱的校验规则
                    // rules.add(new ValidationUtil.ValidationRule<>(
                    //     porter.getCardNo(),
                    //     email -> email != null,
                    //     "Email格式不正确"
                    // ));

                    // 执行校验
                    return ValidationUtil.validateFields(rules);
                },
                (data) -> {
                    // 数据处理
                    if (projectStage.getId() != null) {
                        ProjectStage origin = projectStageRepository.getById(projectStage.getId());
                        projectStage.setCompanyId(origin.getCompanyId());

                    }
                    projectStage.setStatus((byte) 1);

                    return projectStage;
                },
                (data) -> {
                    // 保存后续逻辑在这写
                });
    }


    /**
     * 修改状态
     * @param model
     * @param projectStage
     * @return
     * @throws Exception
     */
    @RequestMapping("/state")
    @RequiresPermissions("pcs:projectStage:state")
    public ResponseEntity<?> state(Model model, @EntityParam ProjectStage projectStage) throws Exception {

        return appService.save(projectStageRepository, (data) -> {

            // 校验数据
            return ValidationUtil.validateFields(new ArrayList<>());

        }, (data) -> {
            // 处理数据
            ProjectStage origin = projectStageRepository.getById(projectStage.getId());
            origin.setStatus(projectStage.getStatus());

            return origin;

        }, (data) -> {
            // 更新后操作
        });

    }


}
